package cn.com.blueInfo.bpm.person.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.person.entity.GroupFormula;
import cn.com.blueInfo.bpm.person.enums.GroupFormulaFilterType;
import cn.com.blueInfo.bpm.person.mapper.GroupFormulaMapper;
import cn.com.blueInfo.bpm.person.repository.GroupFormulaRepository;
import cn.com.blueInfo.bpm.person.request.GroupFormulaDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 群组公式表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:28:09
 */
@Log4j2
@Service
public class GroupFormulaRepositoryImpl
        extends ServiceImpl<GroupFormulaMapper, GroupFormula>
        implements GroupFormulaRepository, BaseRepository<GroupFormula, GroupFormulaDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增群组公式
     * @param groupFormula 群组公式参数
     */
    @Override
    public Integer createGroupFormula(GroupFormula groupFormula) {
        return sqlExecuteResult(save(groupFormula));
    }

    /**
     * 批量新增群组公式
     * @param groupFormulaList 群组公式参数列表
     */
    @Override
    public Integer batchCreateGroupFormula(List<GroupFormula> groupFormulaList) {
        return sqlExecuteResult(saveBatch(groupFormulaList), groupFormulaList.size());
    }

    /**
     * 删除群组公式
     * @param groupFormulaId 群组公式ID
     */
    @Override
    public Integer deleteGroupFormula(Long groupFormulaId) {
        GroupFormula groupFormula = new GroupFormula();
        groupFormula.setId(groupFormulaId);
        groupFormula.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(groupFormula));
    }

    /**
     * 批量删除群组公式
     * @param groupFormulaIdList 群组公式ID列表
     */
    @Override
    public Integer batchDeleteGroupFormula(List<Long> groupFormulaIdList) {
        List<GroupFormula> groupFormulaList = new ArrayList<>();
        for (Long groupFormulaId : groupFormulaIdList) {
            GroupFormula groupFormula = new GroupFormula();
            groupFormula.setId(groupFormulaId);
            groupFormula.setDeleted(BaseDeleted.DELETED);
            groupFormulaList.add(groupFormula);
        }
        return sqlExecuteResult(removeBatchByIds(groupFormulaList), groupFormulaList.size());
    }

    /**
     * 更新部分群组公式
     * @param groupFormula 群组公式参数
     */
    @Override
    public Integer updateGroupFormulaPartial(GroupFormula groupFormula) {
        validateUpdateId(groupFormula.getId());
        LambdaUpdateWrapper<GroupFormula> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(GroupFormula::getId, groupFormula.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部群组公式
     * @param groupFormula 群组公式参数
     */
    @Override
    public Integer updateGroupFormula(GroupFormula groupFormula) {
        validateUpdateId(groupFormula.getId());
        return sqlExecuteResult(updateById(groupFormula));
    }

    /**
     * 查询一条群组公式
     * @param groupFormulaId 群组公式ID
     */
    @Override
    public GroupFormula getGroupFormulaById(Long groupFormulaId) {
        LambdaQueryWrapper<GroupFormula> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GroupFormula::getId, groupFormulaId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询群组公式
     * @param groupFormulaQueryDTO 群组公式参数
     */
    @Override
    public Page<GroupFormula> getGroupFormulaPage(GroupFormulaDTO.QueryDTO groupFormulaQueryDTO) {
        Page<GroupFormula> groupFormulaPage = new Page<>(
                groupFormulaQueryDTO.getPage(), groupFormulaQueryDTO.getSize());
        LambdaQueryWrapper<GroupFormula> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, groupFormulaQueryDTO);

        BaseDeleted deleted = groupFormulaQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(groupFormulaPage, queryWrapper);
        } else {
            return page(groupFormulaPage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 群组公式 数量
     * @param groupFormulaQueryDTO 群组公式参数
     */
    @Override
    public Long getGroupFormulaCount(GroupFormulaDTO.QueryDTO groupFormulaQueryDTO) {
        LambdaQueryWrapper<GroupFormula> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, groupFormulaQueryDTO);

        BaseDeleted deleted = groupFormulaQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 群组公式
     * @param groupFormulaQueryDTO 群组公式参数
     */
    @Override
    public List<GroupFormula> getGroupFormulaList(GroupFormulaDTO.QueryDTO groupFormulaQueryDTO) {
        LambdaQueryWrapper<GroupFormula> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, groupFormulaQueryDTO);

        BaseDeleted deleted = groupFormulaQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<GroupFormula> queryWrapper,
                              GroupFormulaDTO.QueryDTO groupFormulaQueryDTO) {

        Long groupId = groupFormulaQueryDTO.getGroupId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(groupId), GroupFormula::getGroupId, groupId);

        GroupFormulaFilterType filterType = groupFormulaQueryDTO.getFilterType();
        queryWrapper.eq(ObjectUtils.isNotEmpty(filterType), GroupFormula::getFilterType, filterType);

        String filterBound = groupFormulaQueryDTO.getFilterBound();
        queryWrapper.eq(StringUtils.isNotBlank(filterBound), GroupFormula::getFilterBound, filterBound);

        String filterSql = groupFormulaQueryDTO.getFilterSql();
        queryWrapper.eq(StringUtils.isNotBlank(filterSql), GroupFormula::getFilterSql, filterSql);

        Long draftOrgId = groupFormulaQueryDTO.getDraftOrgId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(draftOrgId), GroupFormula::getDraftOrgId, draftOrgId);

        addQueryWrapper(queryWrapper, groupFormulaQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<GroupFormula, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                GroupFormula::getOrderNumber,
                GroupFormula::getTenantId,
                GroupFormula::getApplicationId,
                GroupFormula::getStatus,
                GroupFormula::getDeleted,
                GroupFormula::getDeleteUserId,
                GroupFormula::getDeleteTime,
                GroupFormula::getRemark,
                GroupFormula::getCreateUserId,
                GroupFormula::getCreateTime,
                GroupFormula::getUpdateUserId,
                GroupFormula::getUpdateTime,
                GroupFormula::getExtend1,
                GroupFormula::getExtend2,
                GroupFormula::getExtend3);
    }

}
